package com.teamwe.movecard.controller;

import com.teamwe.movecard.domain.Problem;
import com.teamwe.movecard.domain.Result;
import com.teamwe.movecard.service.ProblemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "题目接口")
@Slf4j
@RestController
@RequestMapping("/problem")
public class ProblemController {

    @Autowired
    ProblemService problemService;

    /**
     * 功能描述：获取题目列表，该接口开放给小程序使用，用于获取游戏时使用的题目。
     * @param grade
     * @return
     */
    @ApiOperation(value = "微信用户获取数据接口", notes = "微信前端提交年级的信息后可以获取到相应年级的随机题目列表，" +
            "每次返回10条题目数据，不足10条时则返回查询到的实际数据")
    @GetMapping("/wx")
    public Result wxGetProblemList(@ApiParam(value = "年级",required = true) @RequestParam String grade){
        Result result = new Result(true);
        List<Problem> problemes = null;
        try {
            problemes = problemService.getRandomProblemList(grade);
            result.addParam("problemes", problemes);
            Integer[] indexes = new Integer[problemes.size()];
            Random random = new Random();
            for(int i = 0; i < problemes.size(); i++){
                String  problem = problemes.get(i).getProblem();
                String[] token = problem.split("\\s");
                indexes[i] = random.nextInt(token.length);
            }
            result.addParam("indexes", indexes);
            result.setDescribe(String.format("请求成功！收到的请求参数为: grade: %s",grade));
        } catch (Exception e) {
            result.setStatus(false);
            result.setDescribe(String.format("请求失败！收到的请求参数为: grade: %s",grade));
        } finally {
            return result;
        }
    }

    /**
     * 根据年级信息分页查询，如果年级信息为null的话，直接返回不分年级信息的分页查询结果
     * @param grade
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "分页查询题目", notes = "根据提交的年级信息，页码和页大小进行分页查询，其中年级信息不是必要的。" +
            "如果没有年级信息的话，返回的数据就是不分年级信息的，即只是总的题目列表的分页查询，不分类")
    @GetMapping("/bgm")
    public Result bgmGetProblemList(@ApiParam(value = "年级",required = false) @RequestParam(required = false) String grade,
                                    @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
                                    @ApiParam(value = "页码",required = true) @RequestParam Integer pageNum){
        Result result = new Result(false);
        if(pageNum <= 0 && pageSize <= 0){
            result.setDescribe(String.format("pageNum和pageSize不能为0！ 收到的参数为：grade: %s, pageSize: %d, pageNum: %d", grade, pageSize, pageNum));
            return  result;
        } else {
            if (pageNum <= 0 || pageSize <= 0) {
                if (pageNum <= 0) {
                    result.setDescribe(String.format("pageNum不能为0！ 收到的参数为：grade: %s, pageSize: %d, pageNum: %d", grade, pageSize, pageNum));
                    return  result;
                }else {
                    result.setDescribe(String.format("pageSize不能为0！ 收到的参数为：grade: %s, pageSize: %d, pageNum: %d", grade, pageSize, pageNum));
                    return  result;
                }
            }
        }
        result.setStatus(true);
        Page<Problem> problems = problemService.getProblemByPage(grade, pageSize, pageNum-1);
        result.addParam("problems", problems);
        String describe = String.format("收到的参数为：grade: %s, pageSize: %d, pageNum: %d", grade, pageSize, pageNum);
        result.setDescribe(describe);
        return result;
    }

    /**
     * 功能描述：提供给后台管理系统使用的添加题目的接口,主要是用来保存题目的，可以添加新的题目或者更新题目
     * @param problem
     * @return
     */
    @ApiOperation(value = "保存题目",notes = "提交与题目相关的信息，然后后台会保存这个数据，如果题目中包含题目的Id，那就会更新该Id相对应的题目信息，" +
            "如果不包含题目Id，则会在数据库中插入新的题目信息")
    @PostMapping
    public Result submitProblem(@ApiParam(value = "题目信息",required = true)  @RequestBody Problem problem){
        Result result = new Result(true);
        if(problem.getProblem().trim().equals("") || problem.getProblem() == null || problem.getGrade().trim().equals("") || problem.getGrade() == null){
           result.setStatus(false);
           result.setDescribe(String.format("题目或年级不能为空！ 收到的参数为：problem: %s, grade: %s", problem.getProblem(), problem.getGrade()));
           return result;
        }
        Problem p = problemService.saveProblelm(problem);
        int proId = p.getProId();
        result.addParam("proId", proId);
        String describe = String.format("收到的参数为：problem: %s, grade: %s", problem.getProblem(), problem.getGrade());
        result.setDescribe(describe);
        return result;
    }

    /**
     * 单选删除
     * @param proId
     * @return
     */
    @ApiOperation(value = "删除单个题目", notes = "只能删除一条数据，所以只需要提交一个题目的id就可以了，会将数据库中相对应的题目删除")
    @DeleteMapping
    public Result deleteProblem(@ApiParam(value = "题目id",required = true) @RequestBody(required = true) Integer proId){
        Result result = new Result(true);
        try {
            problemService.deleteProblem(proId);
            String describe = String.format("删除成功！收到的参数为：proId: %s", proId);
            result.setDescribe(describe);
        } catch (Exception e) {
            result.setStatus(false);
            result.setDescribe(String.format("该序号对应的题目不存在！收到的参数为：proId: %s", proId));
        } finally {
            return result;
        }
    }

    /**
     * 批量删除
     * @param proIds
     * @return
     */
    @ApiOperation(value = "批量删除题目", notes = "提交一个包含题目id的数组，可以把相对应的这些题目一次性删除")
    @DeleteMapping("/batch")
    public Result batchDeletion(@ApiParam(value = "要删除的题目的Id数组",required = true) @RequestBody List<Integer> proIds){
        Result result = new Result(true);
        try {
            problemService.batchDeletion(proIds);
            String describe = String.format("删除成功! 收到的参数为：proIds: %s", proIds);
            result.setDescribe(describe);
        } catch (Exception e) {
            result.setStatus(false);
            result.setDescribe(String.format("删除失败！收到的参数为：proIds: %s", proIds));
        } finally {
            return  result;
        }
    }
}
